package com.elec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.R;
import com.elec.domain.entity.AlarmRecord;
import com.elec.mapper.AlarmRecordMapper;
import com.elec.mapper.UsageDataMapper;
import com.elec.domain.entity.UsageData;
import com.elec.service.UsageDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 存储用户的实时用电数据(UsageData)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 13:43:00
 */
@Service("usageDataService")
public class UsageDataServiceImpl extends ServiceImpl<UsageDataMapper, UsageData> implements UsageDataService {

    @Resource
    private AlarmRecordMapper alarmRecordMapper;
    @Override
    public R add(UsageData usageData) {
        boolean save = save(usageData);
        if (usageData.getVoltage() > 220 || usageData.getVoltage() < 180){
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setAlarmType("电压");
            alarmRecord.setDescription("电压异常");
            alarmRecord.setDataId(usageData.getDataId());
            alarmRecordMapper.insert(alarmRecord);
        }
        if (usageData.getCurrent() > 70 || usageData.getCurrent() < 0){
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setAlarmType("电流");
            alarmRecord.setDescription("电流异常");
            alarmRecord.setDataId(usageData.getDataId());
            alarmRecordMapper.insert(alarmRecord);
        }
        if (usageData.getPower() > 1000 || usageData.getPower() < 0){
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setAlarmType("功率");
            alarmRecord.setDescription("功率异常");
            alarmRecord.setDataId(usageData.getDataId());
            alarmRecordMapper.insert(alarmRecord);
        }
        if (save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R update(UsageData usageData) {
        if (updateById(usageData)) {
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }

    @Override
    public R get(Long id) {
        if (getById(id) != null) {
            return R.success(getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, UsageData usageData) {
        LambdaQueryWrapper<UsageData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(usageData.getUserId()), UsageData::getUserId, usageData.getUserId());
        queryWrapper.orderByDesc(UsageData::getTimestamp);
        Page<UsageData> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return R.success(page.getRecords(), page.getTotal());
    }
}


